package org.testis.gui.mvc.controller.testdesign;

import org.testis.gui.mvc.controller.*;
import org.testis.gui.mvc.model.testdesign.TestCaseGroupModel;
import org.testis.gui.mvc.view.testdesign.TestCaseGroupView;
import org.testis.orm.labelhandler.LabelHandlerFactory;
import java.util.HashMap;
import java.util.LinkedList;
import org.orm.PersistentException;
import org.testis.orm.refinedclasses.TestCaseGroupRefined;
import org.testis.orm.refinedclasses.TestDesignTreeNode;
import org.testis.orm.refinedclasses.TestDesignTreeRefined.TestDesignTreeParameters;
import org.testis.orm.testdesign.TestPartSaver;
import org.testis.utils.tree.TreeNode;

/**
 *
 * @author Cahit Emir Erel
 */
public class TestCaseGroupController extends AbstractController implements SaveAble, Addable {

    public static final String SHOW_ADD_TEST_CASE_GROUP =
            "TestCaseGroupVisibility";
    public static final String CHANGE_NAME = "Name";
    public static final String CHANGE_DESCRIPTION = "Description";
    public static final String CHANGE_RUN_ALWAYS = "RunAlways";
    public static final String CHANGE_TIMEOUT = "TimeOut";
    private TestDesignTreeNode m_selectedNode = null;

    public TestCaseGroupController() {
        super();
    }

    public void operationCloseWindow() {
        this.cleanAll();
    }

    public void operationNewTestCaseGroup() {
        initialize(false);
        m_selectedNode = TestDesignControllerList.newInstance().getTreeController().getSelectedTreeNode();
        TestDesignControllerList.newInstance().getLabelPairSettingsController().operationNewSettings();
        setModelProperty(SHOW_ADD_TEST_CASE_GROUP,
                true);
    }

    public void initialize(boolean editMode) {
        TestCaseGroupModel testCaseGroupModel = new TestCaseGroupModel();
        TestCaseGroupView testCaseGroupView = new TestCaseGroupView(TestDesignControllerList.newInstance().
                getParentFrame(),
                true,
                editMode);
        super.addModel(testCaseGroupModel);
        super.addView(testCaseGroupView);
        testCaseGroupView.pack();
    }

    public void operationEditTestCaseGroup(TestDesignTreeNode node) {
        if (node.getReference().getType() == null) {
            System.out.println("Not found reference type for test case group");
            return;
        } else if (node.getReference().getType().getName() == null) {
            System.out.println(
                    "Not found reference type name for test case group");
            return;
        }
        if (node.getReference().getType().getName().equals("TestCaseGroup")) {
            initialize(true);
            TestCaseGroupRefined designpart = null;
            try {
                designpart = (TestCaseGroupRefined) node.getObject();
            } catch (Exception ex) {
                System.out.println(ex.toString());
            }
            if (designpart == null) {
                System.out.println("Not found design part for test case group");
                return;
            }
            setModelProperty(CHANGE_NAME,
                    designpart.getName());
            setModelProperty(CHANGE_DESCRIPTION,
                    designpart.getReference().getDescription());
            Integer timeout = node.getReference().getParameterIntValue(
                    TestDesignTreeParameters.Timeout.name());
            if (timeout != null) {
                setModelProperty(CHANGE_TIMEOUT,
                        timeout);
            }

            Boolean runAlways = node.getReference().getParameterBooleanValue(
                    TestDesignTreeParameters.RunAlways.name());
            if (runAlways != null) {
                setModelProperty(CHANGE_RUN_ALWAYS,
                        runAlways);
            }

            TestDesignControllerList.newInstance().getLabelPairSettingsController().operationEditSettings(
                    node);

            m_selectedNode = node;
            setModelProperty(SHOW_ADD_TEST_CASE_GROUP, true);
        }
    }

    public void operationAddTestCaseGroup(String name,
            String description,
            String runAlways, LinkedList<String> labels) throws PersistentException {
        TestDesignTreeNode node = TestDesignControllerList.newInstance().
                getTreeController().getSelectedTreeNode();
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("name",
                name);
        params.put("description",
                description);
        params.put("runAlways",
                runAlways);
        params.put("labels", labels);
        TestDesignControllerList.newInstance().getTreeController().operationHandleAdd(
                this, node, params);
    }

    public void operationSaveTestCaseGroup(HashMap<String, Object> params) {
        TestDesignControllerList.newInstance().getTreeController().
                operationHandleSaveClones(this,
                m_selectedNode,
                params);
    }

    @Override
    public boolean operationSave(HashMap<String, Object> params,
            TreeNode node,
            boolean dynamicSaveOnly) {
        return TestPartSaver.operationSave(params,
                (TestDesignTreeNode) node);
    }

    public void operationChangeName(String name) {
        setModelProperty(CHANGE_NAME,
                name);
    }

    public void operationChangeDescription(String description) {
        setModelProperty(CHANGE_DESCRIPTION,
                description);
    }

    public void operationChangeTimeout(String timeout) {
        setModelProperty(CHANGE_TIMEOUT,
                timeout);
    }

    public void operationChangeRunAlways(String runAlways) {
        setModelProperty(CHANGE_RUN_ALWAYS,
                runAlways);
    }

    @Override
    public TestDesignTreeNode operationAddObject(HashMap<String, Object> params, TreeNode node, int childIndex) {
        TestDesignTreeNode nodeToAdd = TestPartSaver.operationAdd(params,
                (TestDesignTreeNode) node, childIndex, "TestCaseGroup");

        return nodeToAdd;
    }

    @Override
    public boolean isValidSave(HashMap<String, Object> params, TreeNode node) {
        return true;
    }

    @Override
    public boolean isValidAdd(HashMap<String, Object> params, TreeNode node) {
        return true;
    }
}
